package io.renren.modules.avanotes.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.*;
import io.renren.common.vo.LocalMarkdownNotesVo;
import io.renren.common.vo.TokenVo;
import io.renren.config.BizConfig;
import io.renren.modules.avanotes.dao.ClassifySubjectDao;
import io.renren.modules.avanotes.dao.FormworkDao;
import io.renren.modules.avanotes.dao.LocalMarkdownNotesDao;
import io.renren.modules.avanotes.dao.SynonymTokensDao;
import io.renren.modules.avanotes.entity.*;
import io.renren.modules.avanotes.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


//@Service("formworkService")//默认只有一个实现，那就不写名称了直接使用默认的名称！
@Service
public class FormworkServiceImpl extends ServiceImpl<FormworkDao, FormworkEntity> implements FormworkService {

    @Autowired
    RedisUtils redisUtils;

    /**
     * 根据ID 从缓存中获取数据
     *
     * @param id
     * @return
     */
    @Override
    public FormworkEntity getEntity(Integer id) throws Exception {
        String sid = "" + id;
        FormworkEntity entity = redisUtils.hGet(BizConfig.CACHE_FORMWORK_ENTITY, sid, new TypeReference<FormworkEntity>(){});
        if (entity == null) {
            //为空，去DB
            entity = getById(id);
            if (entity == null) {
                return null;
            }
            //设置缓存
            redisUtils.hSet(BizConfig.CACHE_FORMWORK_ENTITY, sid, JsonUtils.obj2String(entity));
        }
        return entity;
    }

    @Override
    public void insertIgnoreBatch(List<FormworkEntity> list) {
        this.baseMapper.insertIgnoreBatch(list);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FormworkEntity> page = this.page(
                new Query<FormworkEntity>().getPage(params),
                new QueryWrapper<FormworkEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<FormworkEntity> list4SelectByTitle(String keyword) {
        List<FormworkEntity> list = this.list(new QueryWrapper<FormworkEntity>().like(FormworkEntity.CN_NAME, keyword));
        return list;
    }

}